/*
 * Copyright (c) 2018.
 * This file was written by Fancyears·Milos·Malvis @connor. Any question/bug you can't post to 1243093366@qq.com.
 * ALL RIGHTS RESERVED.
 */
package org.fancyears.renderer.jtable;

import java.util.HashMap;
import java.util.Map;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.fancyears.iservice.CustomEventService;

import lombok.Getter;

/**
 * @author: Fancyears·Milos·Malvis
 * @CreateDate: 2018年12月4日 下午4:27:37
 * @Description:
 */

public class TableDataController extends DefaultTableModel {
	private static final long serialVersionUID = 1L;
	private JTable table;

	@Getter
	private Map<Integer, CustomEventService> service;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public TableDataController() {
		service = new HashMap();
	}

	@Override
	public void setValueAt(Object aValue, int row, int column) {
		System.out.println();
		if (row >= table.getRowCount()) {
			super.setValueAt(aValue, table.getRowCount() - 1, column);
		} else if (column >= table.getColumnCount()) {
			super.setValueAt(aValue, row, table.getColumnCount() - 1);
		} else {
			super.setValueAt(aValue, row, column);
		}
		if (service.containsKey(column)) {
			service.get(column).executeService(aValue, row, column, table);
		}
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		if (1 == columnIndex) {
			return Boolean.class;
		}
		return super.getColumnClass(columnIndex);
	}

	public void setTable(JTable jTable) {
		this.table = jTable;
	}
}